/*
 * @Author: wangming
 * @Date: 2020-09-03 16:33:37
 * @LastEditors: wangming
 * @LastEditTime: 2022-03-28 14:16:59
 * @Description: file content
 */
import { Input } from 'element-ui';
export default {
  components: { eInput: Input },
  props: {
    viewInfo: {
      type: Boolean,
      default: false,
    },
    value: {
      type: null,
      default: '',
    },
    limitNumber: {
      type: [String, Number],
      default: '2',
    },
    showSeparator: {
      type: Boolean,
      default: false,
    },
    max: {
      type: null,
      default: null,
    },
    min: {
      type: null,
      default: null,
    },
  },
  methods: {
    def() {
      this.mode = this.newValue;
    },
    format(num) {
      var reg = /\d{1,3}(?=(\d{3})+$)/g;
      return (num + '').replace(reg, '$&,');
    },
    Nformatter(v = '', flag = false) {
      v += '';
      let limitStr = '';
      if (/^0\d/.test(v)) {
        v = v.replace(/^0/, '');
      }
      let num = v.replace(/[^0-9.-]/g, '');
      if (this.limitNumber == 0) {
        num = v.replace(/[^0-9-]/g, '');
      }
      var arr = num.split('.');
      var fixed = arr[1];
      var firstNum = flag ? this.format(arr[0]) : arr[0];
      if (fixed != undefined) {
        let str = fixed.substring(0, this.limitNumber);
        limitStr = firstNum + '.' + str;
      } else {
        limitStr = firstNum;
      }
      return limitStr.replace(/^\./, '');
    },
  },
  computed: {
    newValue() {
      return this.Nformatter(this.value, this.showSeparator);
    },
  },
  data() {
    return {
      mode: '',
    };
  },
  watch: {
    newValue: {
      handler: 'def',
      immediate: true,
    },
  },
  render(h) {
    const slots = Object.keys(this.$slots)
      .reduce((arr, key) => arr.concat(this.$slots[key]), [])
      .map((vnode) => {
        vnode.context = this._self;
        return vnode;
      });
    return this.viewInfo ? h('div', [...slots, ' ', this.mode]) : h(
      'e-input',
      {
        attrs: {
          ...this.$attrs,
          maxlength: 14
        },
        on: {
          ...this.$listeners,
          blur: async () => {
            let value = this.mode
            if (this.mode !== '') {
              this.mode = (this.mode + '').replace(/\.$/, '');
            }
            if (this.mode === '-') {
              this.mode = ''
              value = this.mode
            }
            let v = value
            const { min, max } = this;
            var result = v.replace(/,/g, "");
            if (result && result !== "-") {
              if (min && Number(result) < min) {
                v = min;
              }
              if (max && Number(result) > max) {
                v = max;
              }
            }
            await this.$nextTick()
            this.$emit('blur', this.Nformatter(v));
          },
          input: (v) => {
            this.$emit('input', this.Nformatter(v));
            this.mode = this.Nformatter(v, this.showSeparator);
          }
        },
        model: {
          value: this.mode,
          callback: (v) => {
            this.mode = v;
          },
          expression: 'mode',
        },
      },
      slots
    );
  },
};
